<template>
    <div>
        <h1>房型添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型名称</td>
                    <td><input type="text" v-model="queryinfo.houseTypeName"></td>
                </tr>
                <tr>
                    <td>门市价</td>
                    <td><input type="text" v-model="queryinfo.housePrice"></td>
                </tr>
                <tr>
                    <td>押金</td>
                    <td>
                        <input type="radio" name="yajin" value="1" v-model="queryinfo.yprice">不需要
                        <input type="radio" name="yajin" value="0" v-model="queryinfo.yprice">需要
                    </td>
                </tr>
                <tr>
                    <td>房型面积</td>
                    <td><input type="text" v-model="queryinfo.area"> </td>
                </tr>
                <tr>
                    <td>可住人数</td>
                    <td><input type="text" v-model="queryinfo.peoples"></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td><input type="file" @change="img">
                        <img :src="queryinfo.photo" alt="" width="100" height="100" v-if="queryinfo.photo != ''">
                    </td>
                </tr>
                <tr>
                    <td>房型介绍</td>
                    <td><input type="text" v-model="queryinfo.desc"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="OK()"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import moment from 'moment';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

let queryinfo: any = reactive({
    "houseTypeName": "",
    "photo": "",
    "housePrice": '',
    "yprice": '',
    "area": '',
    "peoples": '',
    "desc": "",
})

const OK = () => {
    if (queryinfo.houseTypeName == "") {
        alert("房型名称不能为空");
        return;
    }
    if (queryinfo.housePrice == "") {
        alert("门市价不能为空");
        return;
    }
    if (queryinfo.peoples == "") {
        alert("可住人数不能为空");
        return;
    }
    if (queryinfo.photo == "") {
        alert("房型图片不能为空");
        return;
    }
    if (queryinfo.desc == "") {
        alert("房型介绍不能为空");
        return;
    }
    axios({
        url: '/api/House/AddHouseType',
        method: 'post',
        data: queryinfo
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert("房型名称不能重复");
                return;
            }
            if (res.data > 0) {
                alert("添加成功");
                router.push('/houseTypeList');
                return;
            }
            else {
                alert("添加失败");
            }
        })
}

const img = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: '/api/Img/Imgs',
        method: 'post',
        data: f
    })
        .then(res => {
            if (res.data == "不是图片") {
                alert("你上传的不是图片");
                return;
            }
            if (res.data == "大于2M") {
                alert("你上传的图片大于2M");
                return;
            }
            else {
                queryinfo.photo = res.data;
                return;
            }
        })
}

</script>

<style scoped></style>